package com.dragon.web.commons.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;

import com.dragon.common.util.PropertiesLoadUtil;
import com.marvels.common.utils.MarvelsLogUtil;

//@Component
//@ServletComponentScan
//@WebFilter(urlPatterns = "/${platform.name.code}/*",filterName = "corsFilter")
public class CorsFilter implements Filter {
 
	public static  ThreadLocal<String> threadLocalUUID = new ThreadLocal<>();
	
	private static String allowOrigin = "*";
	
	private static String allowMethods = "GET,POST,PUT,DELETE";
	
	private static String allowCredentials = "false";
	
	private static String allowHeaders = "Content-Type,x-requested-with,idFintechUmUser,osVersion,timeStamp,appVersion,terminalNo,accessToken,appKey,channelName,MAC,IMEI,brand,extranetToken,userId";
	
	private static String exposeHeaders;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		String uuid = UUID.randomUUID().toString();
		threadLocalUUID.set(uuid);
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;  
		long start = System.currentTimeMillis();
		MarvelsLogUtil.getInstance().info(uuid + "*************start*****************》》》》》" + request.getRequestURI());
		
		if (StringUtils.isNotEmpty(allowOrigin)) {
			
			List<String> allowOriginList = Arrays.asList(allowOrigin.split(","));
			
			if (CollectionUtils.isNotEmpty(allowOriginList)) {
				String currentOrigin = request.getHeader("Origin");
				if(allowOriginList.contains("*")){
					response.setHeader("Access-Control-Allow-Origin",
							currentOrigin);
				}
			}
		}
		
		if (StringUtils.isNotEmpty(allowMethods)) {
			response.setHeader("Access-Control-Allow-Methods", allowMethods);
		}
		if (StringUtils.isNotEmpty(allowCredentials)) {
			response.setHeader("Access-Control-Allow-Credentials",
					allowCredentials);
		}
		if (StringUtils.isNotEmpty(allowHeaders)) {
			response.setHeader("Access-Control-Allow-Headers", allowHeaders);
		}
		if (StringUtils.isNotEmpty(exposeHeaders)) {
			response.setHeader("Access-Control-Expose-Headers", exposeHeaders);
		}
		chain.doFilter(req, res);
		MarvelsLogUtil.getInstance().info(uuid + "*************end*****************》》》》》" + request.getRequestURI() + "总耗时---》 " + (System.currentTimeMillis() - start) + "状态  》》》》 " + response.getStatus());
		threadLocalUUID.remove();
	}
	
	@Override
	public void destroy() {
	}
}
